package tuanda.soict.firstsun.PresentationPackage;

import java.util.ArrayList;

import tuanda.soict.firstsun.AbilityPackage.Ability;
import tuanda.soict.firstsun.BuildingPackage.Army_Building;
import tuanda.soict.firstsun.BuildingPackage.Building;
import tuanda.soict.firstsun.BuildingPackage.HQ_Building;
import tuanda.soict.firstsun.ClickListenerPackage.ArmyAbilityClickListener;
import tuanda.soict.firstsun.ClickListenerPackage.SelectedArmyClickListener;
import tuanda.soict.firstsun.ClickListenerPackage.SelectedElementClickListener;
import tuanda.soict.firstsun.DataPackage.AbilityList;
import tuanda.soict.firstsun.DataPackage.ArmyDataSet;
import tuanda.soict.firstsun.DataPackage.AttributeList;
import tuanda.soict.firstsun.DataPackage.CommonConstants;
import tuanda.soict.firstsun.LogicPackage.AI_Part;
import tuanda.soict.firstsun.LogicPackage.Army;
import tuanda.soict.firstsun.LogicPackage.AttackSet;
import tuanda.soict.firstsun.LogicPackage.BattleMap;
import tuanda.soict.firstsun.LogicPackage.Environment;
import tuanda.soict.firstsun.LogicPackage.Group;
import tuanda.soict.firstsun.LogicPackage.MovementSet;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.tablelayout.Table;

public class ControlMenu implements GeneralConstants, CommonConstants,
		AttributeList, AbilityList {

	private TiledMapRendererScreen tileScreen;
	private ImageButton move, attack, specialAbility, cancel, attackPoint,
			defensePoint, movePoint, rangePoint, smallCancel, buildingButton,
			armyButton, buyButton, acceptButton, occupyButton, attributeIcon,
			infoButton;
	private ImageButton[] special;
	private TextButton armyActionsButton, buildingActionsButton, menuButton,
			endTurnButton;
	private Label[] label;
	private Table table;
	private Army arm;
	private Ability ability;
	private int abilityIndex;
	private Building building;
	private Environment env;
	private int space;
	private Table buyScreen;
	private AI_Part ai_Part;
	private int selectedElement, selectedArmy, currentScrollIndex,
			numberBuyableArmy;
	private ArrayList<ArmyDataSet> armyGroup;

	public ControlMenu(TiledMapRendererScreen screen) {
		this.tileScreen = screen;
		setNumber(0);
		setSelectedElement(0);
		setCurrentScrollIndex(0);
		setSelectedArmy(0);
		ScenarioInitializer scenarioInitializer = tileScreen
				.getScenarioInitializer();
		Skin skin = tileScreen.getSkin();
		initializeStatusIcon(scenarioInitializer, skin);
		initializeCommonMenu(scenarioInitializer, skin);
		initializeInfoMenu(scenarioInitializer, skin);
		initializeAcceptMenu(scenarioInitializer, skin);
		initializeGeneralArmyMenu(scenarioInitializer, skin);
	}

	public void initializeStatusIcon(ScenarioInitializer scenarioInitializer,
			Skin skin) {
		attackPoint = new ImageButton(scenarioInitializer.getStatusIcon(0, 0));
		defensePoint = new ImageButton(scenarioInitializer.getStatusIcon(0, 1));
		movePoint = new ImageButton(scenarioInitializer.getStatusIcon(1, 0));
		rangePoint = new ImageButton(scenarioInitializer.getStatusIcon(1, 1));
		label = new Label[11];
		for (int i = 0; i < label.length; i++) {
			label[i] = new Label("", skin);
		}
		special = new ImageButton[4];
	}

	public void initializeCommonMenu(ScenarioInitializer scenarioInitializer,
			Skin skin) {
		table = new Table();
		table.bottom().center().setFillParent(true);
		if (Gdx.graphics.getWidth() > 480) {
			setSpace((Gdx.graphics.getWidth() - 480) >> 4);
		} else {
			setSpace(2);
		}
		table.defaults().center().spaceLeft(5 + space);
		if (Gdx.graphics.getWidth() > 320) {
			table.defaults().spaceLeft(getSpace());
		}
		menuButton = new TextButton("MENU", skin);
		menuButton.setClickListener(new ClickListener() {
			@Override
			public void click(Actor actor, float x, float y) {
				clearState();
				TextButton back = new TextButton("BACK", tileScreen.getSkin());
				back.setClickListener(new ClickListener() {

					@Override
					public void click(Actor actor, float x, float y) {
						clearState();
						TalesOfTroubadour game = tileScreen.getMyGame();
						game.setScreen(new StartScreen(null, game));
					}
				});
				table.add(back).minWidth(100);
			}
		});
		endTurnButton = new TextButton("END TURN", skin);
		endTurnButton.setClickListener(new ClickListener() {
			@Override
			public void click(Actor actor, float x, float y) {
				createConfirmScreen("Are you sure you want to end turn ?");
				tileScreen.setScreenState(END_TURN_SCREEN);
			}
		});
	}

	public void initializeGeneralArmyMenu(
			ScenarioInitializer scenarioInitializer, Skin skin) {
		move = new ImageButton(scenarioInitializer.getIcon(0, 0),
				scenarioInitializer.getIcon(2, 0));
		move.setClickListener(new ClickListener() {
			@Override
			public void click(Actor actor, float x, float y) {
				if (arm.isMoveLeft() == true) {
					tileScreen.setScreenState(MOVE_SCREEN);
					createMoveScreen(tileScreen.getMap().getArmy(
							tileScreen.getCurrentX(), tileScreen.getCurrentY()));
					tileScreen.resetAnimation();
				} else {
					createNoticeScreen("You don't have any movement left !");
				}
			}
		});
		attack = new ImageButton(scenarioInitializer.getIcon(0, 2),
				scenarioInitializer.getIcon(2, 2));
		attack.setClickListener(new ClickListener() {
			@Override
			public void click(Actor actor, float x, float y) {
				if (arm.isActionLeft() == true) {
					tileScreen.setScreenState(ATTACK_SCREEN);
					createAttackScreen(tileScreen.getMap().getArmy(
							tileScreen.getCurrentX(), tileScreen.getCurrentY()));
					tileScreen.resetAnimation();
				} else {
					createNoticeScreen("You don't have any action left !");
				}
			}
		});
		infoButton = new ImageButton(scenarioInitializer.getIcon(3, 2),
				scenarioInitializer.getIcon(3, 1));
		infoButton.setClickListener(new ClickListener() {
			@Override
			public void click(Actor actor, float x, float y) {
				if (arm != null) {
					BattleMap map = tileScreen.getAi_Part().getBattleMap();
					if (map.getBuilding(arm.getLocationX(), arm.getLocationY()) != null) {
						createArmyControl(arm, true);
					} else {
						createArmyControl(arm, false);
					}
				}
			}
		});
		cancel = new ImageButton(scenarioInitializer.getIcon(1, 0),
				scenarioInitializer.getIcon(3, 0));
		cancel.setClickListener(new ClickListener() {
			@Override
			public void click(Actor actor, float x, float y) {
				switch (tileScreen.getScreenState()) {
				case MOVE_TARGET_SCREEN:
					createMoveScreen(arm);
					tileScreen.setScreenState(MOVE_SCREEN);
					break;
				case ATTACK_TARGET_SCREEN:
					createAttackScreen(arm);
					tileScreen.setScreenState(ATTACK_SCREEN);
					break;
				case ABILITY_TARGET_SCREEN:
					createSkillScreen(arm);
					tileScreen.setScreenState(ABILITY_SCREEN);
					break;
				default:
					tileScreen.setScreenState(DEFAULT);
					tileScreen.setGeneralState(DEFAULT);
					createMenuScreen(true);
					break;
				}
			}
		});
		specialAbility = new ImageButton(scenarioInitializer.getIcon(0, 3),
				scenarioInitializer.getIcon(2, 3));
		specialAbility.setClickListener(new ClickListener() {
			@Override
			public void click(Actor actor, float x, float y) {
				if (arm.isActionLeft() == true) {
					createAbilityScreen();
					tileScreen.resetAnimation();
				} else {
					createNoticeScreen("You don't have any action left !");
				}
			}
		});
		occupyButton = new ImageButton(scenarioInitializer.getIcon(6, 3),
				scenarioInitializer.getIcon(7, 0));
		occupyButton.setClickListener(new ClickListener() {
			@Override
			public void click(Actor actor, float x, float y) {
				if (arm.isActionLeft() == true) {
					tileScreen.setScreenState(OCCUPY_SCREEN);
					createOccupyScreen(tileScreen.getMap().getArmy(
							tileScreen.getCurrentX(), tileScreen.getCurrentY()));
					tileScreen.resetAnimation();
				} else {
					createNoticeScreen("You don't have any action left !");
				}
			}
		});
		buyButton = new ImageButton(scenarioInitializer.getIcon(1, 4),
				scenarioInitializer.getIcon(0, 4));
		buyButton.setClickListener(new ClickListener() {
			@Override
			public void click(Actor actor, float x, float y) {
				BattleMap map = ai_Part.getBattleMap();
				if (map.getArmy(building.getLocationX(),
						building.getLocationY()) == null) {
					if (ai_Part.getGroup(building.getGroupID()).getNumberArmy() < DEFAULT_MAX_ARMY_PER_GROUP) {
						tileScreen.setGeneralState(BUY_SCREEN);
						createBuyScreen();
					} else {
						createNoticeScreen("You have reached armies limit.");
					}
				} else {
					createNoticeScreen("You can't recruit new army here.");
				}
			}
		});

	}

	public void initializeAcceptMenu(ScenarioInitializer scenarioInitializer,
			Skin skin) {
		acceptButton = new ImageButton(scenarioInitializer.getIcon(6, 0),
				scenarioInitializer.getIcon(4, 3));
		acceptButton.setClickListener(new ClickListener() {
			@Override
			public void click(Actor actor, float x, float y) {
				int type = tileScreen.getScreenState();
				int general = tileScreen.getGeneralState();
				if (general != DEFAULT) {
					switch (general) {
					case BUY_SCREEN:
						if (armyGroup != null) {
							if (ai_Part.isEnoughGold(armyGroup
									.get(selectedArmy).getGoldRequire(),
									building.getGroupID())) {
								ai_Part.addNewArmy(armyGroup.get(selectedArmy),
										building.getGroupID(),
										building.getLocationX(),
										building.getLocationY(),
										getSelectedElement());
								tileScreen.setScreenState(DEFAULT);
								tileScreen.setGeneralState(DEFAULT);
								createMenuScreen(true);
								return;
							} else {
								tileScreen.setScreenState(DEFAULT);
								tileScreen.setGeneralState(DEFAULT);
								createNoticeScreen("You don't have enough gold.");
								return;
							}
						}
					}
				}
				switch (type) {
				case MOVE_TARGET_SCREEN: {
					int locationX = tileScreen.getCurrentX();
					int locationY = tileScreen.getCurrentY();
					if (tileScreen.getMap()
							.getMovementSet(locationX, locationY) != null) {
						tileScreen.setScreenState(MOVING_SCREEN);
						tileScreen.setDestination(locationX, locationY);
						tileScreen.getDrawSpritesHelper().createMovementPath();
						createMenuScreen(true);
						tileScreen.resetAnimation();
					}
				}
					break;
				case ATTACK_TARGET_SCREEN: {
					createConfirmScreen("Are you sure you want to attack ?");
					MovementSet attackSet = tileScreen.getDrawSpritesHelper()
							.getAttackSet();
					if (attackSet != null) {
						ArrayList<AttackSet> temp = attackSet.getAttackSet();
						if (temp.size() > 0) {
							tileScreen.setScreenState(ATTACKING_SCREEN);
							tileScreen.resetAnimation();
						}
					}
				}
					break;
				case OCCUPY_TARGET_SCREEN: {
					int locationX = tileScreen.getCurrentX();
					int locationY = tileScreen.getCurrentY();
					Building build = tileScreen.getMap().getBuilding(locationX,
							locationY);
					if (build != null) {
						if (ai_Part.isInSameSide(build.getGroupID(),
								arm.getGroupID()) == false) {
							tileScreen.setScreenState(OCCUPYING_SCREEN);
							tileScreen.resetAnimation();
						} else if (build.getBuildingType() == HEAD_QUARTER_TYPE) {
							if (((HQ_Building) build).getTurnsBeforeLost() != MANY_TURNS) {
								tileScreen.setScreenState(OCCUPYING_SCREEN);
								tileScreen.resetAnimation();
							}
						}
					}
				}
					break;
				case END_TURN_SCREEN: {
					ai_Part.nextTurn();
					ai_Part.updateLost();
					DrawHelper drawHelper = tileScreen.getDrawSpritesHelper();
					Group win = ai_Part.findWinGroup();
					drawHelper.setWinGroup(win);
					if (win != null) {
						tileScreen.setScreenState(WIN_SCREEN);
					} else {
						if (ai_Part.getPlayerType(ai_Part.getCurrentTurn()) == COMPUTER_PLAYER) {
							tileScreen.setGeneralState(COMPUTER_CONTROL);
							tileScreen.setScreenState(CALCULATING_BUY);
							ai_Part.setBuyCompleted(false);
							ai_Part.setCurrentIndex(0);
							tileScreen.setBuyCompleted(false);
							tileScreen.setCurrentIndex(0);
							clearState();
						} else {
							tileScreen.setScreenState(DEFAULT);
							createMenuScreen(true);
						}
					}
				}
					break;
				case WAIT_SCREEN: {
					arm.setActionLeft(false);
					arm.setMoveLeft(false);
					createMenuScreen(true);
					tileScreen.setScreenState(DEFAULT);
				}
					break;
				case ABILITY_TARGET_SCREEN: {
					tileScreen.setScreenState(ABILITY_ACTING_SCREEN);
					tileScreen.resetAnimation();
					createMenuScreen(true);
				}
					break;
				}
			}
		});
		smallCancel = new ImageButton(scenarioInitializer.getStatusIcon(2, 0),
				scenarioInitializer.getStatusIcon(2, 1));
		smallCancel.setClickListener(new ClickListener() {
			@Override
			public void click(Actor actor, float x, float y) {
				switch (tileScreen.getScreenState()) {
				case MOVE_SCREEN:
					createArmyControlScreen();
					tileScreen.setScreenState(DEFAULT);
					break;
				case ATTACK_SCREEN:
					createArmyControlScreen();
					tileScreen.setScreenState(DEFAULT);
					break;
				case OCCUPY_SCREEN:
					createArmyControlScreen();
					tileScreen.setScreenState(DEFAULT);
					break;
				case ABILITY_SCREEN:
					createAbilityScreen();
					tileScreen.setScreenState(DEFAULT);
					break;
				default:
					tileScreen.setScreenState(DEFAULT);
					tileScreen.setGeneralState(DEFAULT);
					createMenuScreen(true);
					break;
				}
			}
		});

	}

	public void initializeInfoMenu(ScenarioInitializer scenarioInitializer,
			Skin skin) {

		armyActionsButton = new TextButton("ACTIONS", skin);
		armyActionsButton.setClickListener(new ClickListener() {
			@Override
			public void click(Actor actor, float x, float y) {
				createArmyControlScreen();
			}
		});

		buildingActionsButton = new TextButton("ACTIONS", skin);
		buildingActionsButton.setClickListener(new ClickListener() {
			@Override
			public void click(Actor actor, float x, float y) {
				createBuildingControlScreen();
			}
		});
		buildingButton = new ImageButton(
				scenarioInitializer.getStatusIcon(3, 1),
				scenarioInitializer.getStatusIcon(3, 3));
		buildingButton.setClickListener(new ClickListener() {
			@Override
			public void click(Actor actor, float x, float y) {
				createBuildingControl(
						tileScreen.getMap().getBuilding(
								tileScreen.getCurrentX(),
								tileScreen.getCurrentY()), true);
			}
		});
		armyButton = new ImageButton(scenarioInitializer.getStatusIcon(3, 0),
				scenarioInitializer.getStatusIcon(3, 2));
		armyButton.setClickListener(new ClickListener() {
			@Override
			public void click(Actor actor, float x, float y) {
				createArmyControl(
						tileScreen.getMap().getArmy(tileScreen.getCurrentX(),
								tileScreen.getCurrentY()), true);
			}
		});

	}

	public void createNoticeScreen(String text) {
		clearState();
		label[0].setColor(Color.WHITE);
		label[0].setText(text);
		table.add(label[0]);
		table.add(cancel);
	}

	public int getSelectedArmy() {
		return selectedArmy;
	}

	public void setSelectedArmy(int selectedArmy) {
		this.selectedArmy = selectedArmy;
	}

	public int getNumber() {
		return numberBuyableArmy;
	}

	public void setNumber(int num) {
		this.numberBuyableArmy = num;
	}

	public int getCurrentScrollIndex() {
		return currentScrollIndex;
	}

	public void setCurrentScrollIndex(int currentScroll) {
		if (currentScroll < (numberBuyableArmy - 1) && currentScroll > -1) {
			this.currentScrollIndex = currentScroll;
		}
	}

	public int getSelectedElement() {
		return selectedElement;
	}

	public void setSelectedElement(int selected) {
		this.selectedElement = selected;
	}

	public void setArmy(Army arm) {
		if (arm != null) {
			this.arm = arm;
		}
	}

	public Army getArmy() {
		return this.arm;
	}

	public Building getBuilding() {
		return building;
	}

	public void setBuilding(Building build) {
		if (build != null) {
			this.building = build;
		}
	}

	public Environment getEnvironment() {
		return env;
	}

	public void setEnvironment(Environment en) {
		if (en != null) {
			this.env = en;
		}
	}

	public Ability getAbility() {
		return ability;
	}

	public void setAbility(Ability abil) {
		if (abil != null) {
			this.ability = abil;
		}
	}

	public int getAbilityIndex() {
		return abilityIndex;
	}

	public void setAbilityIndex(int abilityIndex) {
		this.abilityIndex = abilityIndex;
	}

	public void setSpace(int s) {
		if (s > -1) {
			this.space = s;
		}
	}

	public int getSpace() {
		return this.space;
	}

	public void setAiPart(AI_Part ai) {
		ai_Part = ai;
	}

	public void clearState() {
		tileScreen.stage.clear();
		table.clear();
		tileScreen.stage.addActor(table);
		for (int i = 0; i < label.length; i++) {
			label[i].setColor(Color.WHITE);
		}
	}

	public void createMenuScreen(boolean isHuman) {
		clearState();
		int screenState = tileScreen.getScreenState();
		int current = ai_Part.getCurrentTurn();
		Group group = ai_Part.getGroup(current);

		setColorOfUnit(current);
		label[0].setText(group.getName().replace("_", " "));
		table.add(label[0]);

		label[5].setText("INCOME: " + group.calculateIncomePerDay());
		table.add(label[5]);

		table.add(new ImageButton(tileScreen.getScenarioInitializer()
				.getStatusIcon(3, 0)));
		label[2].setText("" + group.getNumberArmy() + " / "
				+ DEFAULT_MAX_ARMY_PER_GROUP);
		table.add(label[2]);

		if (isHuman == true && screenState == DEFAULT) {
			table.add(endTurnButton).minWidth(90);
		}

		table.row();

		label[1].setText("GOLD: " + group.getIncome() + "     DAY: "
				+ (ai_Part.getTurnWithID(group.getID()) + 1));
		table.add(label[1]);

		label[4].setText("SIDE: " + group.getSide());
		table.add(label[4]);

		table.add(new ImageButton(tileScreen.getScenarioInitializer()
				.getStatusIcon(3, 1)));
		label[3].setText("" + (group.getNumberBuilding() + 1) + " / "
				+ DEFAULT_MAX_BUILDING_PER_GROUP);
		table.add(label[3]);

		if (isHuman == true && screenState == DEFAULT) {
			table.add(menuButton).minWidth(90);
		}
	}

	public void createArmyControlScreen() {
		clearState();
		setColorOfUnit(arm.getGroupID());
		label[0].setText(arm.getName().replace("_", " "));
		table.add(label[0]);
		if (arm.isMoveLeft() == true) {
			table.add(move);
		}
		if (arm.isActionLeft() == true) {
			table.add(attack);
			table.add(specialAbility);
		}
		table.add(infoButton);
		table.add(cancel);
	}

	public void createConfirmScreen(String text) {
		clearState();
		table.add(cancel);
		label[0].setColor(Color.WHITE);
		label[0].setText(text);
		table.add(label[0]);
		table.add(acceptButton);
	}

	public void createAbilityScreen() {
		clearState();
		table.add(occupyButton);
		try {
			TextureRegion[][] temp;
			// Ability[] ability = arm.getAbility(arm.getLevel());
			Ability[] skillSet = arm.getAbility(9);
			for (int i = 0; i < skillSet.length; i++) {
				if (skillSet[i].getID() != NO_SKILL) {
					temp = TextureRegion.split(new Texture("Skill/"
							+ skillSet[i].getName() + ".png"), 64, 64);
					special[i] = new ImageButton(temp[0][0], temp[1][0]);
					special[i].setClickListener(new ArmyAbilityClickListener(i,
							tileScreen, this, arm));
					table.add(special[i]);
				}
			}
		} catch (Exception e) {
		}
		table.add(cancel);
	}

	public void createBuildingControlScreen() {
		clearState();
		if (building.getBuildingType() == PRODUCE_ARMY_TYPE) {
			table.add(buyButton);
		}
		table.add(cancel);
	}

	public void setColorOfUnit(int id) {
		switch (id) {
		case 0:
			label[0].setColor(Color.RED);
			break;
		case 1:
			label[0].setColor(Color.BLUE);
			break;
		case 2:
			label[0].setColor(Color.MAGENTA);
			break;
		case 3:
			label[0].setColor(Color.GREEN);
			break;
		}
	}

	public void setAttributeImage(int attribute) {
		switch (attribute) {
		case WATER_ATTRIBUTE:
			attributeIcon = new ImageButton(tileScreen.getScenarioInitializer()
					.getStatusIcon(0, 2));
			break;
		case FIRE_ATTRIBUTE:
			attributeIcon = new ImageButton(tileScreen.getScenarioInitializer()
					.getStatusIcon(0, 3));
			break;
		case AIR_ATTRIBUTE:
			attributeIcon = new ImageButton(tileScreen.getScenarioInitializer()
					.getStatusIcon(1, 2));
			break;
		case EARTH_ATTRIBUTE:
			attributeIcon = new ImageButton(tileScreen.getScenarioInitializer()
					.getStatusIcon(1, 3));
			break;
		case LIGHT_ATTRIBUTE:
			attributeIcon = new ImageButton(tileScreen.getScenarioInitializer()
					.getStatusIcon(2, 2));
			break;
		case DARK_ATTRIBUTE:
			attributeIcon = new ImageButton(tileScreen.getScenarioInitializer()
					.getStatusIcon(2, 3));
			break;
		}
	}

	public void createArmyControl(Army ar, boolean hasBuilding) {
		clearState();
		setArmy(ar);
		setColorOfUnit(arm.getGroupID());
		int atk, def, mov, ran;

		label[0].setText(arm.getName().replace("_", " "));
		table.add(label[0]);

		setAttributeImage(ar.getAttribute());
		table.add(attributeIcon);

		label[1].setText(arm.getMinAttack() + " - " + arm.getMaxAttack());
		table.add(attackPoint);
		table.add(label[1]);

		atk = arm.getTempAttack();
		if (atk == 0) {
			label[2].setText("");
		} else if (atk > 0) {
			label[2].setText("(+" + atk + ")");
			label[2].setColor(Color.GREEN);
		} else {
			label[2].setText("(-" + atk + ")");
			label[2].setColor(Color.RED);
		}
		table.add(label[2]).spaceLeft(5);

		if (Gdx.graphics.getWidth() > 320) {
			table.add(movePoint);
			label[6].setText(arm.getMovement() + "");
			table.add(label[6]);

			mov = arm.getTempMovement();
			if (mov == 0) {
				label[7].setText("");
			} else if (mov > 0) {
				label[7].setText("(+" + mov + ")");
				label[7].setColor(Color.GREEN);
			} else {
				label[7].setText("(-" + mov + ")");
				label[7].setColor(Color.RED);
			}
			table.add(label[7]).spaceLeft(5);
		}

		if (ai_Part.getCurrentTurn() == arm.getGroupID()) {
			table.add(armyActionsButton).minWidth(80);
		}

		table.row();

		label[3].setText("Level: " + (arm.getLevel() + 1));
		table.add(label[3]);
		label[10].setText("Exp: " + (arm.getExperience() / 100) + " %");
		table.add(label[10]);
		table.add(defensePoint);

		label[4].setText(arm.getDefense() + "");
		table.add(label[4]);

		def = arm.getTempDefense();
		if (def == 0) {
			label[5].setText("");
		} else if (def > 0) {
			label[5].setText("(+" + def + ")");
			label[5].setColor(Color.GREEN);
		} else {
			label[5].setText("(-" + def + ")");
			label[5].setColor(Color.RED);
		}
		table.add(label[5]).spaceLeft(5);

		if (Gdx.graphics.getWidth() > 320) {
			table.add(rangePoint);
			label[8].setText(arm.getAttackRange() + "");
			table.add(label[8]);

			ran = arm.getTempAttackRange();
			if (ran == 0) {
				label[9].setText("");
			} else if (ran > 0) {
				label[9].setText("(+" + ran + ")");
				label[9].setColor(Color.GREEN);
			} else {
				label[9].setText("(-" + ran + ")");
				label[9].setColor(Color.RED);
			}
			table.add(label[9]).spaceLeft(5);
		}
		if (hasBuilding == true) {
			table.add(buildingButton).minWidth(80);
		}
	}

	public void createBuildingControl(Building bui, boolean hasArmy) {
		clearState();
		setBuilding(bui);
		setColorOfUnit(building.getGroupID());

		label[0].setText(building.getName().replace("_", " "));
		table.add(label[0]);

		label[1].setText(building.getDefense() + "");
		table.add(defensePoint);
		table.add(label[1]);

		if (ai_Part.getCurrentTurn() == building.getGroupID()) {
			table.add(buildingActionsButton).minWidth(80);
		} else {
			table.add(endTurnButton).minWidth(80);
		}

		table.row();

		label[3].setText("Obstacle: ");
		table.add(label[3]);

		label[4].setText("-" + building.getObstaclePoint() + "  movement  ");
		table.add(label[4]).colspan(2);

		if (hasArmy == true) {
			table.add(armyButton).minWidth(80);
		}
	}

	public void createEnvironmentInfo(Environment en) {
		clearState();
		setEnvironment(en);

		label[6].setText(env.getName().replace("_", " "));
		table.add(label[6]);

		label[1].setText(env.getDefense() + "");
		table.add(defensePoint);
		table.add(label[1]);
		table.add(endTurnButton).minWidth(80);

		table.row();

		label[3].setText("Obstacle: ");
		table.add(label[3]);

		label[4].setText("-" + env.getObstaclePoint()
				+ " movement to pass here. ");
		table.add(label[4]).colspan(2);
		table.add(smallCancel);

	}

	public void createMoveScreen(Army ar) {
		clearState();
		setArmy(ar);

		setColorOfUnit(arm.getGroupID());
		label[0].setText(arm.getName().replace("_", " "));
		table.add(label[0]);

		int mov;
		table.add(movePoint);
		label[6].setText(arm.getMovement() + "");
		table.add(label[6]);

		mov = arm.getTempMovement();
		if (mov == 0) {
			label[7].setText("");
		} else if (mov > 0) {
			label[7].setText("(+" + mov + ")");
			label[7].setColor(Color.GREEN);
		} else {
			label[7].setText("(-" + mov + ")");
			label[7].setColor(Color.RED);
		}
		table.add(label[7]);
		table.add(smallCancel);

		table.row();

		label[3].setText("Select any tile in dark areas to move.");
		table.add(label[3]).colspan(5);
	}

	public void createAttackScreen(Army ar) {
		clearState();
		setArmy(ar);

		setColorOfUnit(arm.getGroupID());
		label[0].setText(arm.getName().replace("_", " "));
		table.add(label[0]);

		int atk;
		table.add(attackPoint);
		label[6].setText(arm.getMinAttack() + " - " + arm.getMaxAttack());
		table.add(label[6]);

		atk = arm.getTempAttack();
		if (atk == 0) {
			label[7].setText("");
		} else if (atk > 0) {
			label[7].setText("(+" + atk + ")");
			label[7].setColor(Color.GREEN);
		} else {
			label[7].setText("(-" + atk + ")");
			label[7].setColor(Color.RED);
		}
		table.add(label[7]);
		table.add(smallCancel);

		table.row();

		label[3].setText("Select any enemy in range to attack.");
		table.add(label[3]).colspan(5);
	}

	public void createOccupyScreen(Army ar) {
		clearState();
		setArmy(ar);

		setColorOfUnit(arm.getGroupID());
		label[0].setText(arm.getName().replace("_", " "));
		table.add(label[0]);

		int mov;
		table.add(movePoint);
		label[6].setText(arm.getMovement() + "");
		table.add(label[6]);

		mov = arm.getTempMovement();
		if (mov == 0) {
			label[7].setText("");
		} else if (mov > 0) {
			label[7].setText("(+" + mov + ")");
			label[7].setColor(Color.GREEN);
		} else {
			label[7].setText("(-" + mov + ")");
			label[7].setColor(Color.RED);
		}
		table.add(label[7]);
		table.add(smallCancel);

		table.row();

		label[3].setText("Select any enemy building in range to occupy.");
		table.add(label[3]).colspan(5);
	}

	public void createSkillScreen(Army ar) {
		clearState();
		setArmy(ar);

		setColorOfUnit(arm.getGroupID());
		label[0].setText(arm.getName().replace("_", " "));
		table.add(label[0]);

		ability = arm.getSkill(abilityIndex);
		label[7].setText(ability.getName().replace("_", " "));
		if (ability.getAbilityAffectTo() == CommonConstants.AFFECT_BOTH) {
			label[7].setColor(Color.WHITE);
		} else if (ability.getAbilityAffectTo() == CommonConstants.AFFECT_ALLY
				|| ability.getAbilityAffectTo() == CommonConstants.AFFECT_SELF) {
			label[7].setColor(Color.GREEN);
		} else {// ability.getAbilityAffectTo() == AFFECT_ENEMY
			label[7].setColor(Color.BLACK);
		}
		table.add(label[7]);

		label[6].setText("Range: " + ability.getAbilityRange());
		table.add(label[6]);

		label[8].setText("Area: " + ability.getAbilityArea());
		table.add(label[8]);

		table.add(smallCancel);

		table.row();

		label[3].setText("Select any army in range to using this skill.");
		table.add(label[3]).colspan(6);
	}

	public String getElementName() {
		switch (selectedElement) {
		case WATER_ATTRIBUTE:
			return "WATER";
		case FIRE_ATTRIBUTE:
			return "FIRE";
		case AIR_ATTRIBUTE:
			return "AIR";
		case EARTH_ATTRIBUTE:
			return "EARTH";
		case LIGHT_ATTRIBUTE:
			return "LIGHT";
		case DARK_ATTRIBUTE:
			return "DARK";
		default:
			return "WATER";
		}
	}

	public void createBuyScreen() {
		Group[] gro = ai_Part.getGroup();
		int groID = building.getGroupID();
		createConfirmScreen("GOLD: " + gro[groID].getIncome() + "         "
				+ "Number army: " + gro[groID].getNumberArmy());
		ScenarioInitializer scenarioInitializer = tileScreen
				.getScenarioInitializer();
		Skin skin = tileScreen.getSkin();
		buyScreen = new Table(skin);
		buyScreen.defaults().center().spaceLeft(getSpace());

		Label label1 = new Label("-30% EXP", skin);
		label1.setColor(Color.GREEN);
		buyScreen.add(label1);

		Label label2 = new Label("+5 ATK", skin);
		label2.setColor(Color.GREEN);
		buyScreen.add(label2);

		Label label3 = new Label("+1 MOVE", skin);
		label3.setColor(Color.GREEN);
		buyScreen.add(label3);

		Label label4 = new Label("+5 DEF", skin);
		label4.setColor(Color.GREEN);
		buyScreen.add(label4);

		Label label5 = new Label("+7000 EXP", skin);
		label5.setColor(Color.GREEN);
		buyScreen.add(label5);

		Label label6 = new Label("x2 KillGold", skin);
		label6.setColor(Color.GREEN);
		buyScreen.add(label6);

		buyScreen.row();

		ImageButton[] element = new ImageButton[NUMBER_ATTRIBUTE];
		for (int elementIndex = 0; elementIndex < NUMBER_ATTRIBUTE; elementIndex++) {
			element[elementIndex] = scenarioInitializer
					.getElement(elementIndex);
			element[elementIndex]
					.setClickListener(new SelectedElementClickListener(
							elementIndex, this));
			buyScreen.add(element[elementIndex]);
		}

		buyScreen.row();

		Group[] group = ai_Part.getGroup();
		armyGroup = ArmyDataSet.SOLDIER.getArmyGroup(
				group[building.getGroupID()].getType(),
				((Army_Building) building).getArmyProduceType());
		setNumber(armyGroup.size());

		ArmyDataSet selectedArmyDataSet = armyGroup.get(selectedArmy);
		label[1].setText(selectedArmyDataSet.getName().replace("_", " "));
		buyScreen.add(label[1]);

		ImageButton next, prev;
		next = new ImageButton(scenarioInitializer.getIcon(0, 5),
				scenarioInitializer.getIcon(1, 5));
		prev = new ImageButton(scenarioInitializer.getIcon(1, 1),
				scenarioInitializer.getIcon(4, 1));

		label[6].setText("");
		buyScreen.add(label[6]);

		Label[] goldLabel = new Label[2];
		for (int i = 0; i < 2; i++) {
			goldLabel[i] = new Label(armyGroup.get(currentScrollIndex + i)
					.getGoldRequire() + "G", skin);
			goldLabel[i].setColor(Color.YELLOW);
			buyScreen.add(goldLabel[i]);
		}

		label[9].setText("");
		buyScreen.add(label[9]);

		label[8].setText(getElementName().replace("_", " "));
		buyScreen.add(label[8]);
		buyScreen.row();
		buyScreen.add(new ImageButton(
				scenarioInitializer.getCreatureTextureRegion()[building
						.getGroupID()][selectedArmy]));

		next.setClickListener(new ClickListener() {
			@Override
			public void click(Actor actor, float x, float y) {
				setCurrentScrollIndex(currentScrollIndex + 1);
				createBuyScreen();
			}
		});
		prev.setClickListener(new ClickListener() {
			@Override
			public void click(Actor actor, float x, float y) {
				setCurrentScrollIndex(currentScrollIndex - 1);
				createBuyScreen();
			}
		});
		ImageButton[] armyImage = new ImageButton[2];
		buyScreen.add(prev);
		for (int i = 0; i < 2; i++) {
			armyImage[i] = new ImageButton(
					scenarioInitializer.getCreatureTextureRegion()[building
							.getGroupID()][i + currentScrollIndex]);
			armyImage[i].setClickListener(new SelectedArmyClickListener(i
					+ currentScrollIndex, this));
			buyScreen.add(armyImage[i]);
		}
		buyScreen.add(next);

		buyScreen.add(scenarioInitializer.getElement(getSelectedElement()));
		buyScreen.row().spaceTop(space);

		label[2].setText("ATK: " + selectedArmyDataSet.getMinAttack() + "-"
				+ selectedArmyDataSet.getMaxAttack());
		label[2].setColor(Color.GREEN);
		buyScreen.add(label[2]);

		label[3].setText("DEF: " + selectedArmyDataSet.getDefense());
		label[3].setColor(Color.GREEN);
		buyScreen.add(label[3]);

		label[4].setText("MOVE: " + selectedArmyDataSet.getMovement());
		label[4].setColor(Color.GREEN);
		buyScreen.add(label[4]);

		label[5].setText("RANGE: " + selectedArmyDataSet.getAttackRange());
		label[5].setColor(Color.GREEN);
		buyScreen.add(label[5]);

		label[7].setText("GOLD: " + selectedArmyDataSet.getGoldRequire());
		label[7].setColor(Color.GREEN);
		buyScreen.add(label[7]);

		label[10].setText("RANK: " + selectedArmyDataSet.getRank());
		label[10].setColor(Color.GREEN);
		buyScreen.add(label[10]);

		buyScreen.top().setFillParent(true);
		tileScreen.stage.addActor(buyScreen);
	}
}
